/**
 * Created with IntelliJ IDEA.
 * Description: 时间复杂度与空间复杂度的测试学习
 */



public class Test {
    //空间复杂度测试

    //计算阶乘递归Factorial的空间复杂度？
    public static long factorial2(int N) {
        return N < 2 ? N : factorial2(N - 1) * N;
    }

    //计算fibonacci的空间复杂度？
    public static int[] fibonacci1(int n) {
        int[] fibArray = new int[n + 1];
        fibArray[0] = 0;
        fibArray[1] = 1;
        for (int i = 2; i <= n; i++) {
            fibArray[i] = fibArray[i - 1] + fibArray[i - 2];
        }
        return fibArray;
    }

    //计算 bubbleSort 的空间复杂度
    void bubbleSort(int[] array) {
        for (int end = array.length; end > 0; end--) {
            boolean sorted = true;
            for (int i = 1; i < end; i++) {
                if (array[i - 1] > array[i]) {
                    Swap(array, i - 1, i);
                    sorted = false;
                }
            }
            if(sorted == true) {
                break;
            }
        }
    }

    //时间复杂度测试
    //示例7
    //计算斐波那契递归fibonacci的时间复杂度和空间复杂度
    int fibonacci(int N) {
        return N < 2 ? N : fibonacci(N - 1) + fibonacci(N - 2);
    }

    //示例6
    // 计算阶乘递归factorial的时间复杂度？
    long factorial(int N) {
        return N < 2 ? N : factorial(N - 1) * N;
    }

    //示例5
    int binarySearch(int[] array, int value) {
        int begin = 0;
        int end = array.length - 1;
        while (begin <= end) {
            int mid = begin + ((end-begin) / 2);
            if (array[mid] < value)
                begin = mid + 1;
            else if (array[mid] > value)
                end = mid - 1;
            else
                return mid;
        }
        return -1;
    }

    //示例4
    void bubbleSort(int[] array) {
        for (int end = array.length; end > 0; end--) {
            boolean sorted = true;
            for (int i = 1; i < end; i++) {
                if (array[i - 1] > array[i]) {
                    Swap(array, i - 1, i);
                    sorted = false;
                }
            }
            if
            (sorted == true) {
                break;
            }
        }
    }

    //示例3
    void func4(int N) {
        int count = 0;
        for (int k = 0; k < 100; k++) {
            count++;
        }
        System.out.println(count);
    }

    //示例2
    public static void func3(int N, int M) {
        int count = 0;
        for (int k = 0; k < M; k++) {
            count++;
        }
        for( int k = 0; k < N; k++){
            count++;
        }
        System.out.println(count);
    }

    //示例1
    public static void func2(int N) {
        int count = 0;
        for (int k = 0; k < 2 * N; k++) {
            count++;
        }
        int M = 10;
        while ((M--) > 0) {
            count++;
        }
        System.out.println(count);
    }
}
